package com.zhuzi.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONReader;

import java.util.List;
import java.util.Map;

/**
 * jpa结果转换
 */
public class JpaRowUtils {

    /**
     * List<Map>==> List<T>
     *
     * @param mapList 地图列表
     * @param type    类型
     * @return {@link List}<{@link T}>
     */
    public static <T> List<T> toList(List<Map<String, Object>> mapList, Class<T> type) {
        return JSON.parseArray(JSON.toJSONString(mapList), type, JSONReader.Feature.SupportSmartMatch);
    }

    /**
     * {@link Map}==>{@link  T}
     *
     * @param map  地图
     * @param type 类型
     * @return {@link T}
     */
    public static <T> T toSingle(Map<String, Object> map, Class<T> type) {
        return JSON.parseObject(JSON.toJSONString(map), type, JSONReader.Feature.SupportSmartMatch);
    }
}
